//
//  LXLineViewController.m
//  bgms_d
//
//  Created by lachesis-zhu on 2019/3/6.
//  Copyright © 2019 lachesis-zhu. All rights reserved.
//

#import "LXLineViewController.h"
#import "LXCycleView.h"
#import "LXFoodItem.h"
#import "LXDevSettingModel.h"

@interface LXLineViewController () <LXCycleViewDelegate>

@property (nonatomic, strong) NSMutableArray *dataArr;
@property (strong, nonatomic) LXCycleView *cycleView;
@property (nonatomic, assign) BOOL displayOnce;
@property (nonatomic, assign) NSInteger displayIndex;
@property (nonatomic, strong) NSMutableDictionary *tempDic;
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation LXLineViewController

#pragma mark -  public method
- (void)refreshLineDat:(NSMutableArray *)array hasCut:(BOOL)cut
{
    self.cycleView.showDataDic = self.showDataDic;
    self.cycleView.startDate = self.showDataDic[@"startDate"];
    [self.cycleView refreshView:array hasCut:cut];
    self.tempArray = array;
    [self refreshIndex:self.displayIndex];
}
#pragma mark -  life cycle
- (void)dealloc
{
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initializationData];
    [self addNotification];

    [self loadUI];

    [self loadViewDatas];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [self.boardView addSubview:self.cycleView];
    [self.cycleView startPlay];
    [self.cycleView drawXandY];
    //[self refreshIndex:self.displayIndex];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

#pragma mark -  over write

#pragma mark -  setter & getter
- (void)setDevDataArray:(NSArray *)devDataArray
{
    _devDataArray = devDataArray;
}

- (void)setTempArray:(NSMutableArray *)tempArray
{
    _tempArray = tempArray;
}

- (void)setShowDataDic:(NSMutableDictionary *)showDataDic
{
    _showDataDic = showDataDic;
}
#pragma mark -  private method
- (void)initializationData
{
    self.displayIndex = 1;
    self.dataArr = [[NSMutableArray alloc] initWithObjects:kAppBackgroundColor, kAppBackgroundColor, kAppBackgroundColor, kAppBackgroundColor, kAppBackgroundColor, kAppBackgroundColor, nil];
//    self.timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(addData) userInfo:nil repeats:YES];
//    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
}

- (void)loadUI
{
    self.view.backgroundColor = kAppBackgroundColor;
    [self buildNavi];
    
    [self.pageControlArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *button = obj;
        [button setBackgroundImage:[UIImage imageWithColor:kAppNormalTextColor] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageWithColor:kAppThemeColor] forState:UIControlStateSelected];
        [button setTitleColor:[UIColor colorWithHexString:@"0C0C0C"] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithHexString:@"9B9B9B"] forState:UIControlStateSelected];
        if (idx == 0) {
            button.selected = YES;
        }
    }];
}

- (void)loadViewDatas
{
    
}

- (void)buildNavi
{
    
}

- (void)addData {
    for (NSInteger i = 0; i < 6; i ++) {
        LXFoodItem *item = [[LXFoodItem alloc] init];
        item.tempNum = [NSString stringWithFormat:@"%u", arc4random() % 2 + 150];
        [self.tempArray replaceObjectAtIndex:i withObject:item];
    }
    [self.cycleView refreshView:self.tempArray hasCut:NO];
    [self refreshIndex:self.displayIndex];
}

- (void)refreshIndex:(NSInteger)index {
    if (index == 0) {
        index = 6;
    }
    index -= 1;
    if (self.tempArray.count <= index) {
        return;
    }
    LXFoodItem *item = [self.tempArray objectAtIndex:index];
    if ([item.tempNum integerValue] < 600 && ![item.tempNum isEqualToString:@"0"]) {
        self.tempDataLabel.text = [[LXDevSettingModel shareSettingModel] tempCTempF:item.tempNum];
        self.presetLabel.hidden = YES;
    } else {
        self.tempDataLabel.text = @"-|N/A";
        self.presetLabel.hidden = NO;
    }
    if (item.rangeTemp) {
        self.tempAddLabel.hidden = NO;
        self.tempAddImage.hidden = NO;
        self.tempAddLabel.text = [[LXDevSettingModel shareSettingModel] tempCTempF:item.minNum];
    } else {
        self.tempAddLabel.hidden = YES;
        self.tempAddImage.hidden = YES;
    }
    self.tempMusLabel.text = [[LXDevSettingModel shareSettingModel] tempCTempF:item.setNum];
}
#pragma mark -  notification
- (void)addNotification
{
    
}

#pragma mark -  network


#pragma mark -  event handle
- (void)homeDown:(UIButton *)sender
{
    
}

- (IBAction)pageControlDown:(UIButton *)sender {
    [self.cycleView goToPage:sender.tag];
}
#pragma mark -  delegate
- (NSInteger)countOfCycleView:(LXCycleView *)cycleView {
    return self.dataArr.count;
}

- (void)cycleView:(LXCycleView *)cycleView willDisplayImageView:(UIView *)showView index:(NSInteger)index
{
    self.displayIndex = index;
}

- (void)cycleView:(LXCycleView *)cycleView didDisplayTitleLabel:(UILabel *)titleLabel index:(NSInteger)index{
    
}

- (void)cycleView:(LXCycleView *)cycleView currentIndex:(NSInteger)index
{
    [self.pageControlArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *button = obj;
        if (idx == index) {
            button.selected = YES;
        } else {
            button.selected = NO;
        }
    }];
    if (index == 5) {
        index = 0;
    } else {
        index += 1;
    }
    [self refreshIndex:index];
}
#pragma mark -  lazy load
- (NSMutableDictionary *)tempDic {
    if (!_tempDic) {
        _tempDic = [[NSMutableDictionary alloc] init];
    }
    return _tempDic;
}

- (LXCycleView *)cycleView {
    if (!_cycleView) {
        _cycleView = [[LXCycleView alloc] initWithFrame:self.boardView.bounds delegate:self];
        _cycleView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
        _cycleView.startDate = self.showDataDic[@"startDate"];
        _cycleView.showDataDic = self.showDataDic;
    }
    return _cycleView;
}
#pragma mark -  memoryWarning

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
